﻿using System.IO;
using System.Xml;

public class ReadXML  {

    /// <summary>
    /// 根据路径来创建xml文件
    /// </summary>
    /// <param name="filePath">文件路径</param>
    public void XmlCreate(string filePath)
    {
        if (!File.Exists(filePath))
        {
            // 创建xml文档实例
            XmlDocument xmlDoc = new XmlDocument();
            // 创建根节点
            XmlElement root = xmlDoc.CreateElement("GameMessages");

            XmlElement river = xmlDoc.CreateElement("river");
            river.InnerText = "0";
            XmlElement direction = xmlDoc.CreateElement("direction");
            direction.InnerText = "0";
            XmlElement quality = xmlDoc.CreateElement("imageQuality");
            quality.InnerText = "High";

            // 排序
            root.AppendChild(river);
            root.AppendChild(direction);
            root.AppendChild(quality);

            xmlDoc.AppendChild(root);
            xmlDoc.Save(filePath);

            UnityEditor.AssetDatabase.Refresh();	// 编辑器模式下刷新显示
        }
    }
    public void XmlCreateShipTypes(string filePath)
    {
        if (!File.Exists(filePath))
        {
            // 创建xml文档实例
            XmlDocument xmlDoc = new XmlDocument();
            // 创建根节点
            XmlElement root = xmlDoc.CreateElement("ShipTypes");
            for (int i = 1; i <= 16; i++)
            {
                int index = i;
                XmlElement type = xmlDoc.CreateElement("shiptypes"+index);
                type.InnerText = "true";
                root.AppendChild(type);
            }

            //XmlElement river = xmlDoc.CreateElement("river");
            //river.InnerText = "0";
            //XmlElement direction = xmlDoc.CreateElement("direction");
            //direction.InnerText = "0";
            //XmlElement quality = xmlDoc.CreateElement("imageQuality");
            //quality.InnerText = "High";

            //// 排序
            //root.AppendChild(river);
            //root.AppendChild(direction);
            //root.AppendChild(quality);

            xmlDoc.AppendChild(root);
            xmlDoc.Save(filePath);

            UnityEditor.AssetDatabase.Refresh();	// 编辑器模式下刷新显示
        }
    }
    public string XmlReadShipTypes(int index, string path)
    {
        if (File.Exists(path))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNodeList root = xmlDoc.SelectSingleNode("ShipTypes").ChildNodes;
            foreach (XmlElement item in root)
            {
                if (item.Name == "shiptypes" + index)
                {
                    return item.InnerText;
                }
            }
            UnityEditor.AssetDatabase.Refresh();
        }
        return null;
    }
    public void XmlUpdateShipTypes(string path, int index, string value)
    {
        if (File.Exists(path))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);

            XmlNodeList root = xmlDoc.SelectSingleNode("ShipTypes").ChildNodes;

            foreach (XmlElement item in root)
            {
                if (item.Name == "shiptypes" + index)
                {
                    item.InnerText = value;
                    break;
                }
            }
            xmlDoc.Save(path);
        }
    }
    public bool CheckExistFiles(string filePath) {
        if (File.Exists(filePath))
        {
            return true;
        }
        return false;
    }
    /// <summary>
    /// 读取xml
    /// </summary>
    /// <param name="path">路径</param>
    public string XmlRead(string partName, string path)
    {
        if (File.Exists(path))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNodeList root = xmlDoc.SelectSingleNode("GameMessages").ChildNodes;
            foreach (XmlElement item in root)
            {
                if (item.Name == partName)
                {
                    return item.InnerText;
                }
            }
            UnityEditor.AssetDatabase.Refresh();
        }
        return null;
    }
    /// <summary>
    /// 更新指定id的xml数据
    /// </summary>
    /// <param name="path"></param>
    /// <param name="id"></param>
    public void XmlUpdate(string path, string name, string value)
    {
        if (File.Exists(path))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);

            XmlNodeList root = xmlDoc.SelectSingleNode("GameMessages").ChildNodes;

            foreach (XmlElement item in root)
            {
                if (item.Name == name)
                {
                    item.InnerText = value;
                    break;
                }
            }
            xmlDoc.Save(path);
        }
    }
}
